package serialization;


import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


public class XMLCodegSerializer implements CodegSerializer
{
    public void save(Object object, File file)
    {
        JAXBContext context;
        Marshaller m;
        try
        {
            context = JAXBContext.newInstance(object.getClass());
            m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(object, file);
        } catch (JAXBException e)
        {
            e.printStackTrace();
        }
    };
    
    public Object load(File file, Class<?> cls)
    {
        JAXBContext context;
        Unmarshaller unmarshaller;
        try
        {
            context = JAXBContext.newInstance(cls);
            unmarshaller = context.createUnmarshaller();
            unmarshaller.setSchema(null);
            return unmarshaller.unmarshal(file);
        } catch (JAXBException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}